# tools/write_file.py
""" 
author：Men Dong Yang
create_time：2025/5/7 14:50:45
description：
"""
import yaml
from requests import Response

from tools.read_file import read_yaml


def write_token_to_headers(r: Response,platform):
    """platform存储的token是client用户端,admin管理端"""
    # 调用函数把结果中token取出赋值给headers
    token = f'Bearer {r.json()["data"]["token"]}'
    # 读取headers.yaml
    headers = read_yaml('config/headers.yaml')
    # 修改读取到的authori-zation的值
    headers[platform]['authori-zation'] = token

    # 重新存入headers.yaml
    write_yaml('config/headers.yaml', headers)


def write_yaml(filename, data):
    """

    :param filename: yaml文件路径
    :param data: 要写入的数据
    :return:
    """
    with open(filename, 'w') as f:
        yaml.safe_dump(data, f)
